﻿Imports ATS.Business.Entities
Imports ATS.Data
Imports System.Transactions
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Public Class CategoryComponent

#Region "CRUD"
    Public Function CreateCategory(_category As Category) As Category
        Return CreateNewCategory(_category)
    End Function

    Public Function ReadCategory(_category As String) As Category
        ' Retrieve data.
        Dim dac As New AssetDataAccess()
        Try
            Return dac.ReadCategory(_category)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Public Function ReadAllCategories() As List(Of Category)
        ' Retrieve data.
        Dim dac As New AssetDataAccess()
        Try
            Return dac.ReadAllCategories()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Public Function GetAssetClassList() As List(Of String)
        Dim dac As New AssetDataAccess
        Try
            Return dac.GetAllCategoryClass()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Public Function UpdateCategoryRecord(_category As Category) As Category
        Return UpdateCategoryRecordItem(_category)
    End Function


    Public Sub DeleteCategory(_category As Category)
        Dim dac As New AssetDataAccess
        Try
            dac.DeleteCategory(_category)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try

    End Sub
#End Region

#Region "Support Procedures"

    Private Function UpdateCategoryRecordItem(_category As Category) As Category

        Using ts As New TransactionScope(TransactionScopeOption.Required)
            Try
                Dim dac As New AssetDataAccess()
                dac.UpdateCategory(_category)
                ts.Complete()
            Catch ex As TransactionAbortedException
                Console.WriteLine("TransactionAbortedException Message: {0}", ex.Message)
                Throw ex
            Catch ex As ApplicationException
                Console.WriteLine("ApplicationException Message: {0}", ex.Message)
                Throw ex
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Throw ex
            End Try
        End Using
        Return _category
    End Function

    Private Function CreateNewCategory(_category As Category) As Category
        Dim dac As New AssetDataAccess()
        Try
            Return dac.CreateCategory(_category)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function
#End Region

End Class
